//
//  ST_GitSegmentControl.swift
//  btvideo
//
//  Created by mac on 2020/6/29.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_GitSegmentControl: UIView {
    
    static let lineHeight = 3.cgFloat
    
    static let lineWidth = 18.cgFloat
    
    static let height = 22 + (3 + ST_GitSegmentControl.lineHeight)*2
    
    var leftEdge = 15.cgFloat

    var space = 30.cgFloat
    
    var selectedTextColor = UIColor.init(hex: 0xFEC207)
    
    var normalTextColor = UIColor.init(hex: 0xACB4C0)
    
    var textNormalFont = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
    
    var textSelectedFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
    
    
    
    var titleArray = [String](){
        didSet{
            
            self.removeSubviews()
            var last_btn:UIButton?
            for (idx,item) in titleArray.enumerated(){
                
                let itemBtn = UIButton.init(type: UIButton.ButtonType.custom)
                itemBtn.setTitleForAllStates(item)
                itemBtn.setTitleColor(selectedTextColor, for: UIControl.State.selected)
                itemBtn.setTitleColor(normalTextColor, for: UIControl.State.normal)
                itemBtn.titleLabel?.font = textNormalFont
                itemBtn.isSelected = idx == 0
                
                self.addSubview(itemBtn)
                
                itemBtn.snp.makeConstraints { (make) in
                    if let last = last_btn{
                        make.left.equalTo(last.snp.right).offset(space)
                    }else{
                        make.left.equalToSuperview().offset(leftEdge)
                    }
                    make.top.bottom.equalToSuperview()
                }
                itemBtn.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
                
                last_btn = itemBtn
                
                itemBtn.tag = baseTag + idx
                
                
                if idx == 0{
                    itemBtn.titleLabel?.font = textSelectedFont
                    itemBtn.isSelected = true
                    last_selected_btn = itemBtn
                    self.addSubview(line)
                    line.snp.makeConstraints { (make) in
                        make.width.equalTo(ST_GitSegmentControl.lineWidth)
                        make.height.equalTo(ST_GitSegmentControl.lineHeight)
                        make.centerX.equalTo(itemBtn)
                        make.bottom.equalToSuperview()
                    }
                }
            }
        }
    }
    
    fileprivate var last_selected_btn:UIButton?
    
    fileprivate let baseTag = 1237
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = selectedTextColor
        control.cornerRadius = 1
        return control
    }()
    
    
    var selected_closure:((Int)->())?
}

//MARK:- Event
extension ST_GitSegmentControl{
    
    func selectBtn(index:Int){
        
        let find = self.subviews.first { (v) -> Bool in
            return v != line && v.tag == self.baseTag + index
        }
        
        if let btn = find as? UIButton{
            self.clickedLayout(sender: btn)
        }
    }
    fileprivate func clickedLayout(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        line.snp.remakeConstraints { (make) in
            make.width.equalTo(ST_GitSegmentControl.lineWidth)
            make.height.equalTo(ST_GitSegmentControl.lineHeight)
            make.centerX.equalTo(sender)
            make.bottom.equalToSuperview()
        }
        
        UIView.animate(withDuration: 0.25) {
            self.layoutIfNeeded()
        }
        
        sender.titleLabel?.font = textSelectedFont
        last_selected_btn?.titleLabel?.font = textNormalFont
        
        last_selected_btn?.isSelected = false
        sender.isSelected = true
        last_selected_btn = sender
        
    }
    
    @objc fileprivate func click_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        self.clickedLayout(sender: sender)
        
        self.selected_closure?(sender.tag - self.baseTag)
        
    }
    
}
